import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";
import { useUserStore } from "@/stores/modules/user";
// const useUserStore = useUserStore();
/**
 * @name 文件上传模块
 */
// 客户新增/查询
export const uploadImg = params => {
  return http.post(PORT1 + `/client/submit`, params);
};

// 客户列表
export const customerList = params => {
  const userStore = useUserStore(); // 获取用户状态管理store实例
  if (params) {
    Object.assign(params, {
      username: userStore.username
    })
    return http.get(PORT1 + `/client`, params);
  }
  return http.get(PORT1 + `/client`, { username: userStore.username });
};

// 修改客户
export const customerEdit = params => {
  return http.put(PORT1 + `/client`, params);
};

// 删除客户
export const customerDel = params => {
  return http.delete(PORT1 + `/client`, params);
};

// 阶段管理列表
export const stageList = params => {
  const userStore = useUserStore(); // 获取用户状态管理store实例
  if (params) {
    Object.assign(params, {
      username: userStore.username
    })
    return http.get(PORT1 + `/client`, params);
  }
  return http.get(PORT1 + `/periodtime`, { username: userStore.username });
};

// 论文管理列表
export const thesisList = params => {
  const userStore = useUserStore(); // 获取用户状态管理store实例
  if (params) {
    Object.assign(params, {
      username: userStore.username
    })
    return http.get(PORT1 + `/thesis`, params);
  }
  return http.get(PORT1 + `/thesis`, { username: userStore.username });
};

// 论文管理修改
export const thesisEdit = params => {
  return http.put(PORT1 + `/thesis`, params);
};

// 论文管理下载客户|论文文件
export const thesisDownload = async params => {
  try {
    // 调用下载接口
    const response = await http.get(PORT1 + `/thesis/download`, { params }, { responseType: 'blob' })
    // 检查响应头是否存在
    const contentType = response.type?.['content-type'] || 'application/octet-stream';
    const blob = new Blob([response], { type: contentType });
    // 创建 URL 并触发下载
    const downloadUrl = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = downloadUrl;
    link.download = params.fileName; // 可根据需要从 response.headers['content-disposition'] 提取文件名
    document.body.appendChild(link);
    link.click();
    link.remove();
    window.URL.revokeObjectURL(downloadUrl);
  } catch (error) {
    console.error('下载文件时出错:', error);
    alert('下载失败，请稍后再试');
  }
};

// 审核管理---审核操作
export const thesisStatuss = params => {
  return http.put(PORT1 + `/thesis/status`, params, { cancel: false });
};
// 金钱查询
// 首页查询
// 通知查询
